clear all
set maxvar 30000
version 14
capture log close
set more off

****************************************************************************************************
* -----   Customize the paths and options:   ----- 
****************************************************************************************************
*cd  "Data\"
global MY_IN_PATH   "C:\Users\benjamin.balsmeier\Dropbox\Research\REStat\data"
global MY_OUT_PATH  "C:\Users\benjamin.balsmeier\Dropbox\Research\REStat"
*global MY_TEMP_PATH "..."

global MY_OUT_FILE  ${MY_OUT_PATH}out.dta
global MY_LOG_FILE  ${MY_OUT_PATH}cr_g2.log

log using "${MY_LOG_FILE}", text replace
****************************************************************************************************
* import CS data
*************************************************************************************************

use ${MY_IN_PATH}/Compustat.dta, clear

* merge CPI
merge n:1 year using ${MY_IN_PATH}/cpi1913_2017.dta, keep(3) nogen
replace cpi = cpi/100

gen lemp = log(emp+1)
gen lsale_def = log(sale*1000/cpi)
gen lrd_def = log(xrd/cpi)
replace lrd_def = 0 if lrd_def ==.
gen lppe_def = log(ppent/cpi)

** Merge nberces 
destring sic, replace 
merge n:1 sic year using ${MY_IN_PATH}/output.dta, keep(3) nogen

gen output =  log(lout_d) 

* merge tech prox measure 
destring gvkey, replace
merge 1:1 gvkey year using ${MY_IN_PATH}/is.dta, keep(3) nogen 

gen tp5y = 1-tp_raw5_2020 
gen lnpat = log(npat2020+1)

*****************************************************************************
** industry specific cycles graph
**************************************************************
					
* merge industry specific cycles
merge n:1 sic year using ${MY_IN_PATH}/ind_specific_cyc_sic4_out.dta, keep(3) nogen 


reghdfe fr_npat_new5_2020 ticyc* , absorb(gvkey) cluster(sic) 
gen b1 = .
local j = 1
foreach var of varlist ticyc1-ticyc10 {
replace b1 = _b[`var'] in `j'
local j = `j'+1
}							


gen se1 = .
local j = 1
foreach var of varlist ticyc1-ticyc10 {
replace se1 = _se[`var'] in `j'
local j = `j'+1
}

gen min951 = b1 - 1.96 * se1
gen max951 = b1 + 1.96 * se1


gen t = .
local i = 1
foreach num of numlist 1 2 3 4 5 6 7 8 9 10{
replace t = `num' in `i'
local i = `i'+1
}

					
gen h = _n 
replace t = 0 if h ==11
foreach var of varlist b1 min951 {
replace `var' = . if t == 0
}

drop if t==0	
sort t				
scatter b1 min951 max951 t, msymbol(circle none none) msize(small small small) mcolor(navy black black) /// 
connect(direct dashed dashed) lpattern(line dash dash) lcolor (navy black black) ///
legend(order(1 2) row(1) position(6) label(1 "β-coefficients") label(2 "95%-confidence-interval")) /// 
xscale(range(0.5/10.5) titlegap(3) outergap(4)) xlabel(1(1)10) xtitle("Years between Industry Trough and Peak")  ///
yscale(titlegap(3)) ylabel(-0.16(0.04)0.10) ytitle("coefficient size") yline(0, lcolor(navy)) ///
title("Innovative Search over the Industry Cycle") ///
name(control, replace)		
